extends Button

class_name UnitButton

signal selected

var unit : Unit

func _ready():
	connect("pressed", self, "_on_UnitButton_pressed")

func _on_UnitButton_pressed():
	emit_signal("selected", unit)

func get_unit_from_name(unit_name: String) -> Unit:
	var _unit := Unit.new()
	for u in _unit.type_string:
		var ul : Array = get_units_list((u as String))
		if ul.has(unit_name):
			var unit_path : String = "res://Units/" + u + "/" + unit_name + ".tscn"
			var unit_tscn : PackedScene = load(unit_path)
			_unit = unit_tscn.instance()
	return _unit

func get_units_list(type: String) -> Array:
	var dir := Directory.new()
	var arr := []
	var path : String = "res://Units/" + type
	if dir.open(path) == OK:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		while file_name != "" and file_name != "." and file_name != "..":
			arr.append(file_name.split(".")[0])
			file_name = dir.get_next()
	return arr
